package fussbudget;

import java.awt.Container;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import fussbudget.SQL.FBSQL;
import fussbudget.SQL.SQLUtility;
import fussbudget.gui.GUI;

public class FussBudgetMain extends JApplet {
	private static final String	LOOKANDFEEL	= "System";
	private static GUI			gui			= null;
	private static Budget		budget		= null;
	private static SQLUtility	sql			= null;

	public static PaymentEntitySet allEntitySet = new PaymentEntitySet();
	public static PaymentEntitySet payeeSet = new PaymentEntitySet();
	public static PaymentEntitySet payerSet = new PaymentEntitySet();

	public void init() {
		setupUISkin();
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					try {
						FBSQL.connectDataBase("fussBudget");
					} catch (Exception e) {
						e.printStackTrace();
					}
					budget = new Budget();
					gui = new GUI(FussBudgetMain.this);
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't complete successfully");
		}
	}

	public void main() {
		setupUISkin();
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					budget = new Budget();
					gui = new GUI(FussBudgetMain.this);
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't complete successfully");
		}
	}

	/**
	 * Create the frame.
	 */
	public FussBudgetMain() {
		// gui = new GUI(null);
	}

	public JFrame findParentFrame() {
		Container c = FussBudgetMain.this;
		while (c != null) {
			if (c instanceof JFrame)
				return (JFrame) c;

			c = c.getParent();
		}
		return (JFrame) null;
	}

	public static void setupUISkin() {
		String lookAndFeel = null;

		if (LOOKANDFEEL != null) {
			if (LOOKANDFEEL.equals("Metal")) {
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
				// an alternative way to set the Metal L&F is to replace the
				// previous line with:
				// lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";

			}

			else if (LOOKANDFEEL.equals("System")) {
				lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			}

			else if (LOOKANDFEEL.equals("Motif")) {
				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			}

			else if (LOOKANDFEEL.equals("GTK")) {
				lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
			}

			else {
				System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL);
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			}

			try {

				UIManager.setLookAndFeel(lookAndFeel);

			}

			catch (ClassNotFoundException e) {
				System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel);
				System.err.println("Did you include the L&F library in the class path?");
				System.err.println("Using the default look and feel.");
			}

			catch (UnsupportedLookAndFeelException e) {
				System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform.");
				System.err.println("Using the default look and feel.");
			}

			catch (Exception e) {
				System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason.");
				System.err.println("Using the default look and feel.");
				e.printStackTrace();
			}
		}
	}

}
